<?php
/**
 * ExtDataProxyBase class file
 *
 * @author TrueSoftware B.V. (The Netherlands)
 * @link http://www.ext4yii.com
 * @copyright Copyright 2007-2010 TrueSoftwareB.V. all rights reserved.
 * @license http://www.ext4yii.com/license/
 *          EVALUATION VERSION - NOT FOR COMMERCIAL USE OR DISTRIBUTION.
 * @version 0.7.2
 */
 
 
abstract class ExtDataProxyBase extends ExtObservable
{
    private $_apis_class_types = array('ExtAPIParameter');
    private $_connection_class_types = array('ExtConnection');

    public function parseNode(ExtHTMLParserNode $node,
        ExtASTWalker $astwalker)
    {
        parent::parseNode($node, $astwalker);
        if(!$this->parseApiMode($node, $astwalker))
            $this->parseAPIs($node, $astwalker);
        $this->parseConnection($node, $astwalker);
    }

    private function parseApiMode($node,$astwalker)
    {
        $controller = strtolower(str_ireplace('controller', '',trim($this->getControllerName())));
        if($controller != "")
        {
            $cmds = array('read','create','update','destroy');
            $api = new ExtJavaScriptObject();

            foreach($cmds as $item)
            {
                $cfg = new ExtJavaScriptObject();
                $cfg->addString('method', 'POST');
                $cfg->addString('url', Yii::app()->urlManager->createUrl($controller.'/_'.$item));
                $api->addMember($item, $cfg->render());
            }
            $this->setJavaScriptConfig('api', $api->render());
            $this->getController()->addAfterInitScript(sprintf('Ext.StoreMgr.get("%s").proxy.on("exception",ext4yii.crudExceptionHandler)',$this->parent->getID()));
            return true;
        }
        return false;
    }

    private function parseAPIs($node,$astwalker)
    {
        $apis = $node->findPropertyByName('apis');
        if(!is_null($apis))
        {
            $items=parent::parseNodeWidthTag($apis,
                $astwalker,
                true,
                array('asobject'=>false),
                $this,
                $this->_apis_class_types);
            $this->setObjectConfig('api', $items);
            $apis->shiftUp();
        }
    }

    private function parseConnection($node,$astwalker)
    {
        $conn = $node->findPropertyByName('connection');
        if(!is_null($conn))
        {
            $items=parent::parseNodeWidthTag($conn,
                $astwalker,
                true,
                array('asobject'=>false),
                $this,
                $this->_connection_class_types);
            $conn->shiftUp();
        }
    }

    public function renderConfig($asobject=true)
    {
        $this->deleteConfig('apiMode');
        $this->deleteConfig('controllername');
        return parent::renderConfig($asobject);
    }

    public function createContainer(ExtHTMLParserNode $node)
    {
        $node->shiftUp();
        return null;
    }
}
?>
